package com.persicadev.khayyam;

import java.util.Random;

import com.persicadev.khayyam.R;
import com.persicadev.khayyam.IPoemDb.Poem;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class RandomActivity extends Activity implements OnClickListener {

	public static final String KEY_ID = "id";
	
	private Poem mPoem = null;
	private Poem textPoem = null;
	private SharedPreferences sp;
	
	private Typeface typeFace = null;
	private boolean start = false;
	
	private void setup(TextView view) {
		view.setText(PersianReshape.reshape(view.getText().toString()));
		view.setTypeface(typeFace);
		view.setOnClickListener(this);
	}
	
	private void renew() {
		int id = getIntent().getIntExtra(KEY_ID, -1);
		if (id==-1) id = new Random().nextInt(KhayyamActivity.poemDb.totalPoems());
		CheckBox tb = (CheckBox) findViewById(R.id.random_bookmark);
		setup(tb);
		if (sp.contains(String.valueOf(id).trim())) {
			tb.setChecked(true);
		}
		else {
			tb.setChecked(false);
		}
		setup(tb);
		TextView text = (TextView) findViewById(R.id.random_id);
		text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/BZar.ttf"));
		text.setText(PersianReshape.reshape(String.valueOf(id)));
		mPoem = KhayyamActivity.poemDb.getPoem(id);
		textPoem=KhayyamActivity.poemDb.getPoem(id);
		//((RobaeiView) findViewById(R.id.random_poem)).setPoem(mPoem,true);	
		((RobaeiView) findViewById(R.id.random_poem)).setPoem(mPoem,false);
		
		final String PoemText = textPoem.getVerse(0).getPart1().toString()+"\n"+textPoem.getVerse(0).getPart2().toString()+"\n\n"+textPoem.getVerse(1).getPart1().toString()+"\n"+textPoem.getVerse(1).getPart2().toString();
		ImageButton btnShare = (ImageButton)findViewById(R.id.btn_random_Share);
	    btnShare.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
		        sharingIntent.setType("text/plain");
		        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
		        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,PoemText);
		        
		        startActivity(Intent.createChooser(sharingIntent, PersianReshape.reshape("اشتراک گذاری")));
			}
		});}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.randompoem);
		sp = getSharedPreferences(KhayyamActivity.PREFERENCES, MODE_PRIVATE);
		typeFace = Typeface.createFromAsset(getAssets(), "fonts/BZar.ttf");
		renew();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (start) renew();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.random_bookmark:
			Editor editor = sp.edit();
			if (((CheckBox) v).isChecked()) {
				editor.putInt(String.valueOf(mPoem.getId()).trim(), mPoem.getId()).commit();
			}
			else {
				editor.remove(String.valueOf(mPoem.getId()).trim()).commit();
			}
			break;
		}
	}

	@Override
	public void onBackPressed() {
		start = true;
		super.onBackPressed();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

}
